package com.bomberdroid.ui;

import java.util.logging.Level;
import java.util.logging.Logger;

import android.content.Context;
import android.graphics.Canvas;
import android.os.Handler;
import android.util.Log;
import android.view.SurfaceHolder;

/**
 * The Main thread which contains the game loop. The thread must have access to
 * the surface view and holder to trigger events every game tick.
 */

public class MainThread extends Thread {

	private static final String TAG = MainThread.class.getSimpleName();

	// Surface holder that can access the physical surface
	private SurfaceHolder surfaceHolder;
	// The actual view that handles inputs
	// and draws to the surface
	private GamePanel gamePanel;

	private Handler handler;

	private Context context;

	
	public final static int RUNNING = 1;
	public final static int PAUSED = 2;
	public final static int STOPPED = 3;
	
	public int state = MainThread.STOPPED;

	// for consistent rendering
	private long sleepTime;
	// amount of time to sleep for (in milliseconds)
	private long delay = 70;
	// This is the most important part of the code. It is invoked when the call
	// to start() is made from the SurfaceView class. It loops continuously until the game is
	// finished or the application is suspended.
	private long beforeTime;

	private Canvas c;

	public MainThread(SurfaceHolder surfaceHolder, Context context, Handler handler, GamePanel gamePanel) {
		super();
		this.surfaceHolder = surfaceHolder;
		this.gamePanel = gamePanel;

		this.handler = handler;
		this.context = context;

	}

	/**
	 * Loop of the game
	 */
	@Override
	public void run() {
		// long tickCount = 0L;
		Log.d(TAG, "Starting game loop");

		while (state == RUNNING) {

			beforeTime = System.nanoTime();
			c = null;

			try {

				c = surfaceHolder.lockCanvas(null);

				synchronized (surfaceHolder) {
					if (state == RUNNING)
						gamePanel.onDraw(c);
				}

			}
			catch(Exception e){e.printStackTrace();}
			finally {
				// do this in a finally so that if an exception is thrown
				// during the above, we don't leave the Surface in an
				// inconsistent state
				if (c != null) {
					surfaceHolder.unlockCanvasAndPost(c);
				}
			}
			// Log.d(TAG, "Game loop executed " + tickCount + " times");
		}

		// SLEEP
		// Sleep time. Time required to sleep to keep game consistent
		// This starts with the specified delay time (in milliseconds) then
		// subtracts from that the
		// actual time it took to update and render the game. This allows
		// our game to render smoothly.
		this.sleepTime = delay - ((System.nanoTime() - beforeTime) / 1000000L);

		try {
			// actual sleep code
			if (sleepTime > 0) {
				MainThread.sleep(sleepTime);
			}
		} catch (InterruptedException ex) {
			Logger.getLogger(MainThread.class.getName()).log(Level.SEVERE, null, ex);
		}

		while (state == PAUSED) {
			Log.d("State", "Thread is pausing");
			try {
				MainThread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}
